package com.qifan.energy.adapter;

import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseSectionMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qifan.energy.bean.entity.HomeProductMultiItemEntity;
import com.qifan.energy.R;
import com.qifan.energy.bean.result.HomeListResult;
import com.qifan.energy.global.GlideApp;

import java.util.List;

public class HomeListAdapter extends BaseSectionMultiItemQuickAdapter<HomeProductMultiItemEntity,BaseViewHolder> {

    public HomeListAdapter(List<HomeProductMultiItemEntity> data) {
        super(R.layout.item_home_section_title, data);
        addItemType(HomeProductMultiItemEntity.DISTRIBUTE, R.layout.home_list_item);
        addItemType(HomeProductMultiItemEntity.NORMAL, R.layout.home_list_item);
    }

    @Override
    protected void convertHead(BaseViewHolder helper, HomeProductMultiItemEntity item) {
        if (item.isHeader){
            helper.setText(R.id.title_tv, item.header);
            helper.addOnClickListener(R.id.more_ll);
        }
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, HomeProductMultiItemEntity item) {
        if (!item.isHeader){
            switch (helper.getItemViewType()) {
                case HomeProductMultiItemEntity.DISTRIBUTE:
                    HomeListResult.DistributePmsProductListBean hotProductResult = (HomeListResult.DistributePmsProductListBean)item.getResult();
                    helper.setText(R.id.name_tv,hotProductResult.getName())
                            .setText(R.id.price_tv, String.valueOf(hotProductResult.getPrice()));
                    //初始化UI
                    TextView originalPriceTv1 = helper.getView(R.id.original_price_tv);
                    originalPriceTv1.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    ImageView imageView1 = helper.getView(R.id.product_iv);
                    GlideApp.with(helper.itemView)
                            .load(hotProductResult.getPic())
                            .placeholder(R.mipmap.icon_default_rect)
                            .error(R.mipmap.icon_default_rect)
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(imageView1);
                    break;
                case HomeProductMultiItemEntity.NORMAL:
                    //HomeListResult.NormalPmsProductListBean newProductResult = (HomeListResult.NormalPmsProductListBean)item.getResult();
                    HomeListResult.DistributePmsProductListBean newProductResult = (HomeListResult.DistributePmsProductListBean)item.getResult();
                    helper.setText(R.id.name_tv,newProductResult.getName())
                            .setText(R.id.price_tv, String.valueOf(newProductResult.getPrice()));
                    TextView originalPriceTv2 = helper.getView(R.id.original_price_tv);
                    originalPriceTv2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    ImageView imageView2 = helper.getView(R.id.product_iv);
                    GlideApp.with(helper.itemView)
                            .load(newProductResult.getPic())
                            .placeholder(R.mipmap.icon_default_rect)
                            .error(R.mipmap.icon_default_rect)
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(imageView2);
                    break;
                default:
                    break;
            }
        }
    }
}
